import 'dart:convert';
import 'dart:io';

import 'package:logger/logger.dart';

class F implements LogOutput {
  final File file;
  final bool overrideExisting;
  final Encoding encoding;
  IOSink? _sink;

  F(this.file, {this.overrideExisting = false, this.encoding = utf8});

  @override
  void destroy() async {
    await _sink?.flush();
    await _sink?.close();
  }

  @override
  void init() {
    _sink = file.openWrite(
      mode: overrideExisting ? FileMode.writeOnly : FileMode.writeOnlyAppend,
      encoding: encoding,
    );
  }

  @override
  void output(OutputEvent event) {
    event.lines.forEach((line) {
      _sink?.write('$line \n');
    });
  }
}

main() {
  var p = PrettyPrinter(
      methodCount: 1,
      // number of method calls to be displayed
      errorMethodCount: 2,
      // number of method calls if stacktrace is provided
      lineLength: 60,
      // width of the output
      colors: false,
      // Colorful log messages
      printEmojis: true,
      // Print an emoji for each log message
      printTime: true // Should each log print contain a timestamp
      );
  var sp = SimplePrinter(printTime: true, colors: false);
  var logger = Logger(printer: sp, output: F(File('test.log')));
  logger.d("Logger is working!");
  logger.v("Verbose log");

  logger.d("Debug log");

  logger.i("Info log");

  logger.w("Warning log");

  logger.e("Error log");

  logger.wtf("What a terrible failure log");
}
